import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:honeypotflutter/modules/freePay/api/freePay_api.dart';
import 'package:honeypotflutter/public/public_get.dart';
import 'package:honeypotflutter/publicBussine/bussine_colors.dart';
import 'package:honeypotflutter/publicBussine/bussine_image.dart';
import 'package:honeypotflutter/publicBussine/loginInfo/bussine_userInfo.dart';
import 'package:honeypotflutter/publicBussine/view/bussine_item.dart';
import 'package:honeypotflutter/public/views/public_appBar.dart';
import 'package:honeypotflutter/public/views/public_scrollView.dart';
import 'package:honeypotflutter/router/bussine_router_url.dart';
import 'logic.dart';

class FreePayExchangePage extends StatefulWidget {
  const FreePayExchangePage({Key? key}) : super(key: key);

  @override
  State<FreePayExchangePage> createState() => _FreePayExchangePageState();
}

class _FreePayExchangePageState extends State<FreePayExchangePage> {
  final FreePayExchangeLogic logic = Get.put(FreePayExchangeLogic());

  UserInfoLogic userLogic = Get.find<UserInfoLogic>();

  /*
  * 修改支付方式
  * */
  changePayType(int type) async {
    FreePayApi.exchange(type).then((value) {
      userLogic.changePayType(type);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: HLAppBar(
        navigationTitle: "选择支付方式",
        showBottomLine: true,
      ),
      body: HLScrollView(
        children: [
          MGItems(
            color: MGColors.whileColor,
            padding: EdgeInsets.all(16),
            leadIcon: MGImage.image("freePay_renminbi.png", type: ImagePathType.freePay),
            leadTitle: "账户余额",
            trailingWidget: GetBuilder<UserInfoLogic>(builder: (controller) {
              return Image.asset(
                MGImage.image((controller.userInfo.userFirstChoicePayType == 0) ? "public_pass" : "public_no_pass", type: ImagePathType.public),
                width: 20,
              );
            }),
            action: () {
              changePayType(0);
            },
          ),
          MGItems(
            color: MGColors.whileColor,
            padding: EdgeInsets.all(16),
            leadIcon: MGImage.image("freePay_aliPay.png", type: ImagePathType.freePay),
            leadTitle: "支付宝免密支付",
            trailingWidget: GetBuilder<UserInfoLogic>(builder: (controller) {
              if (controller.userInfo.isAliWithHoldingPaySigned == false) {
                return Expanded(
                    child: Row(
                  children: [
                    Image.asset(MGImage.image("freePay_tuijian.png", type: ImagePathType.freePay), height: 20),
                    Image.asset(MGImage.image("freePag_open", type: ImagePathType.freePay), height: 20),
                  ],
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                ));
              }
              return Expanded(
                  child: Row(
                children: [
                  Image.asset(MGImage.image("freePay_tuijian.png", type: ImagePathType.freePay), height: 20),
                  Image.asset(
                    MGImage.image((controller.userInfo.userFirstChoicePayType == 1) ? "public_pass" : "public_no_pass", type: ImagePathType.public),
                    width: 20,
                  ),
                ],
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
              ));
            }),
            action: () {
              if (userLogic.userInfo.isAliWithHoldingPaySigned == false) {
                HLGet.toNamed(MGRouterUrl.freePay_open);
                return;
              }
              changePayType(1);
            },
          )
        ],
      ),
    );
  }

  @override
  void dispose() {
    Get.delete<FreePayExchangeLogic>();
    super.dispose();
  }
}
